import {onMounted, ref} from "vue";
import admin_service from "../../service/admin_service";
import {ElMessage} from "element-plus";
import router from "../../router/router";

export default function ArticleCreate() {
    // 初始化事件
    onMounted(() => {
        admin_service.getSorts().then(r => {
            let data = r.data
            for (let i = 0; i < data['msg'].length; i++) {
                let dataList = {}
                dataList.value = data['msg'][i]['UrlName']
                dataList.label = data['msg'][i]['Name']
                menuList.value.push(dataList)
            }
        })
    })
    let title_input = ref('')
    let content = ref('')
    let imageList = ref([])
    let menuList = ref([])
    let ArticleSort = ref('')
    const UploadImage = (e, image, files) => {
        console.log('e:', e, 'image"', image, 'files:', files)
    }
    const sendBtn = () => {
        if (title_input.value === '' || content.value === '' || ArticleSort.value === '') {
            ElMessage.error("请填写内容")
        } else {
            let data = {
                title: title_input.value,
                //分类
                class: ArticleSort.value,
                content: content.value
            }
            admin_service.releaseArticle(data).then(r => {
                if (r.data.code === 200) {
                    ElMessage.success(r.data['msg'])
                    router.go(0)
                } else {
                    ElMessage.error(`创建错误：${r.data['msg']}`)
                }
            })
        }

    }
    const handleCopyCodeSuccess = (code) => {
        console.log(code)
    }
    const saveBtn = (text, html) => {
        console.log('text:', text, 'html:', html)
    }

    let editCon80 = ref({
        "width": "80%"
    })

    let editCon100 = ref({
        "width": "100%"
    })
    let editWidth = ref({})
    editWidth.value = editCon80.value
    let SidebarHide = ref({"display": "none"})
    let sidebarShow = ref({
        "display": "flex",
        "justify-content": "center",
        "width": "20%",
        "margin-left": " 20px",
    })
    let sidebarStatus = ref({})
    sidebarStatus.value = sidebarShow.value
    const cancelBtn = () => {
        if (sidebarStatus.value === SidebarHide.value) {
            sidebarStatus.value = sidebarShow.value
            editWidth.value = editCon80.value
        } else {
            sidebarStatus.value = SidebarHide.value
            editWidth.value = editCon100.value
        }
    }
    return {
        title_input,
        content,
        sendBtn,
        UploadImage,
        imageList,
        handleCopyCodeSuccess,
        saveBtn,
        cancelBtn,
        sidebarShow,
        SidebarHide,
        sidebarStatus,
        editWidth,
        menuList,
        ArticleSort
    }
}